# -*- coding: utf-8 -*-

from django.db import models
import hashlib


class Student(models.Model):
	login = models.CharField('Login', max_length=8, unique=True)
	
	email = models.EmailField('E-mail', max_length=45, unique=True);
	
	class Meta:
		db_table = 'students'
		ordering = ('login', )
	
	def __unicode__(self):
		return self.login


class StudentGroup(models.Model):
	students = models.ManyToManyField(Student, db_table='student_groups_have_students')

	name = models.CharField('Nazwa grupy', max_length=45, unique=True)
	
	description = models.TextField('Opis', max_length=300)
	
	class Meta:
		db_table = 'student_groups'
		ordering = ('name', )
	
	def __unicode__(self):
		return self.name + ' - ' + self.description


class Service(models.Model):
	name = models.CharField('Nazwa usługi', max_length=45, unique=True)
	
	description = models.CharField('Opis', max_length=300)
	
	ip_address = models.IPAddressField('Adres IP', blank=True)
	
	script_file = models.FileField('Plik ze skryptem', upload_to='scripts/', blank=True)
	
	reverse_script_file = models.FileField('Plik ze skryptem odwracającym', upload_to='scripts/', blank=True)
	
	class Meta:
		db_table = 'services'
	
	def __unicode__(self):
		return self.name


class StudentGroup(models.Model):
	students = models.ManyToManyField(Student, db_table='student_groups_have_students')

	name = models.CharField('Nazwa grupy', max_length=45, unique=True)
	
	description = models.TextField('Opis', max_length=300)
	
	class Meta:
		db_table = 'student_groups'
		ordering = ('name', )
	
	def __unicode__(self):
		return self.name


class ServiceGroup(models.Model):
	services = models.ManyToManyField(Service, db_table='service_groups_have_services')
	
	name = models.CharField('Nazwa', max_length=45, unique=True)
	
	description = models.CharField('Opis', max_length=300)
	
	class Meta:
		db_table = 'service_groups'
	
	def __unicode__(self):
		return self.name + ' - ' + self.description


class Admin(models.Model):
	service_groups = models.ManyToManyField(ServiceGroup, db_table='admins_have_service_groups', null=True, blank=True)
	
	student_groups = models.ManyToManyField(StudentGroup, db_table='admins_have_student_groups', null=True, blank=True)
	
	login = models.CharField('Login', max_length=45, unique=True)
	
	password = models.CharField('HasĹo', max_length=128)
	
	first_name = models.CharField('ImiÄ', max_length=45)
	
	last_name = models.CharField('Nazwisko', max_length=45)
	
	email = models.EmailField('E-mail', max_length=45, unique=True)
	
	is_superadmin = models.BooleanField('Rola', choices=[(True,'Superadministrator'), (False,'Administrator')])
	
	def comparePassword(self, password):
		return self.password == hashlib.sha1(password).hexdigest()

	def setPassword(self, password):
		self.password = hashlib.sha1(password).hexdigest()

	class Meta:
		db_table = 'admins'


class ActiveSession(models.Model):
	admin_id = models.ForeignKey(Admin)
	
	start_date = models.DateTimeField('Data rozpoczÄcia')
	
	ip_address = models.IPAddressField('Adres IP')
	
	class Meta:
		db_table = 'active_sessions'


class Log(models.Model):
	date = models.DateTimeField('Data')

	description = models.CharField('Opis', max_length=300)

	class Meta:
		db_table = 'logs'
		ordering = ('-date', )


class Task(models.Model):
	services_id = models.ForeignKey(Service)
	
	task_name = models.CharField('Zadanie', max_length=45)
	
	date = models.DateTimeField('Data')
	
	csv_parameters = models.TextField('Paramatry CSV')
	
	class Meta:
		db_table = 'tasks'


class StudentServiceAssociation(models.Model):
	student = models.ForeignKey(Student)
	
	service = models.ForeignKey(Service)
	
	is_active = models.BooleanField('Jest aktywna')
	
	class Meta:
		db_table = 'students_have_services'
